package com.CampaignManager.server;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.NotPersistent;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class Player {
	
	@PrimaryKey
	@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
	private Long id;
	@Persistent
	private String characterName;
	@Persistent
	private String nickName;
	@Persistent
	private String characterDescription;
	@Persistent
	private String email;
	@Persistent
	private Double originLatitude;
	@Persistent
	private Double originLongitude;	
	@Persistent
	private Double destLatitude;
	@Persistent
	private Double destLongitude;
	@Persistent
	private Double milesToTravel;
	@Persistent
	private Date moveStart;
	@Persistent
	private boolean doneMapTutorial = false;
	@Persistent
	private boolean doneJobTutorial = false;
	@Persistent
	private boolean doneEspionageTutorial = false;
	@Persistent
	private boolean doneHireTutorial = false;
	@Persistent
	private Long espionageLevel = 0L;
	@Persistent
	private String faction;
	@Persistent
	private Long level;
	@Persistent
	private Date lastSpentDate;
	@Persistent
	private Date lastForageDate;
	@Persistent
	private String imageURL;
	@Persistent
	private Long narrationPoints = 0L;
	
	@Persistent(mappedBy = "player")
    private List<PlayerResource> resources;	
	public List<PlayerResource> getResources() {
		if (resources == null) resources = new ArrayList<PlayerResource>();
		return resources;
	}
	public PlayerResource getResource(String name) {
		PlayerResource rec = null;
		for (PlayerResource pr : this.getResources()) {
			if (pr.getResource().equalsIgnoreCase(name)) {
				rec = pr;
			}
		}
		if (rec == null) {
			rec = new PlayerResource();
			rec.setResource(name);
			rec.setAmount(0L);
			rec.setOwnerEmail(this.getEmail());
			this.getResources().add(rec);
		}
		return rec;
	}
	
	@Persistent(mappedBy = "player")
    private List<PlayerCity> cities;	
	public List<PlayerCity> getCities() {
		if (cities == null) cities = new ArrayList<PlayerCity>();
		return cities;
	}
	public PlayerCity getCity(String name) {
		PlayerCity city = null;
		for (PlayerCity pc : this.getCities()) {
			if (pc.getCity().equalsIgnoreCase(name)) {
				city = pc;
			}
		}
		if (city == null) {
			city = new PlayerCity();
			city.setCity(name);
			city.setFavor(0L);
			city.setLeverage(0L);
			city.setEmail(this.getEmail());
			this.getCities().add(city);
		}
		return city;
	}
	
	@Persistent(mappedBy = "player")
    private List<Job> jobs;	
	public List<Job> getJobs() {
		if (jobs == null) jobs = new ArrayList<Job>();
		return jobs;
	}
	
	@NotPersistent
	private List<Unit> units = new ArrayList<Unit>();
	public List<Unit> getUnits() {
		return units;
	}
			
	public Long getNarrationPoints() {
		if (narrationPoints == null) narrationPoints = 0L;
		return narrationPoints;
	}
	public void setNarrationPoints(Long narrationPoints) {
		this.narrationPoints = narrationPoints;
	}
	public String getImageURL() {
		if (imageURL == null) imageURL = "<none>";
		return imageURL;
	}
	public void setImageURL(String imageURL) {
		this.imageURL = imageURL;
	}
	public String getNickName() {
		return nickName;
	}
	public void setNickName(String nickName) {
		this.nickName = nickName;
	}
	public Date getLastForageDate() {
		if (lastForageDate == null) lastForageDate = new Date();
		return lastForageDate;
	}
	public void setLastForageDate(Date lastForageDate) {
		this.lastForageDate = lastForageDate;
	}
	public Date getLastSpentDate() {
		return lastSpentDate;
	}
	public void setLastSpentDate(Date lastSpentDate) {
		this.lastSpentDate = lastSpentDate;
	}
	public Long getLevel() {
		if (level == null) level = 0L;
		return level;
	}
	public void setLevel(Long level) {
		this.level = level;
	}
	public String getFaction() {
		if (faction == null) faction = "None";
		return faction;
	}
	public void setFaction(String faction) {
		this.faction = faction;
	}
	public Long getEspionageLevel() {
		if (espionageLevel == null ) espionageLevel = 0L;
		return espionageLevel;
	}
	public void setEspionageLevel(Long espionageLevel) {
		this.espionageLevel = espionageLevel;
	}
	public String getCharacterName() {
		return characterName;
	}
	public void setCharacterName(String characterName) {
		this.characterName = characterName;
	}
	public String getCharacterDescription() {
		return characterDescription;
	}
	public void setCharacterDescription(String characterDescription) {
		this.characterDescription = characterDescription;
	}
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}	
	public Long getId() {
		return id;
	}
	public boolean isDoneMapTutorial() {
		return doneMapTutorial;
	}
	public void setDoneMapTutorial(boolean doneMapTutorial) {
		this.doneMapTutorial = doneMapTutorial;
	}
	public boolean isDoneJobTutorial() {
		return doneJobTutorial;
	}
	public void setDoneJobTutorial(boolean doneJobTutorial) {
		this.doneJobTutorial = doneJobTutorial;
	}
	public boolean isDoneEspionageTutorial() {
		return doneEspionageTutorial;
	}
	public void setDoneEspionageTutorial(boolean doneEspionageTutorial) {
		this.doneEspionageTutorial = doneEspionageTutorial;
	}
	public boolean isDoneHireTutorial() {
		return doneHireTutorial;
	}
	public void setDoneHireTutorial(boolean doneHireTutorial) {
		this.doneHireTutorial = doneHireTutorial;
	}
	public Double getOriginLatitude() {
		return originLatitude;
	}
	public void setOriginLatitude(Double originLatitude) {
		this.originLatitude = originLatitude;
	}
	public Double getOriginLongitude() {
		return originLongitude;
	}
	public void setOriginLongitude(Double originLongitude) {
		this.originLongitude = originLongitude;
	}
	public Double getDestLatitude() {
		if (destLatitude == null) destLatitude = originLatitude;
		return destLatitude;
	}
	public void setDestLatitude(Double destLatitude) {
		this.destLatitude = destLatitude;
	}
	public Double getDestLongitude() {
		if (destLongitude == null) destLongitude = originLongitude;
		return destLongitude;
	}
	public void setDestLongitude(Double destLongitude) {
		this.destLongitude = destLongitude;
	}
	public Double getMilesToTravel() {
		if (milesToTravel == null) milesToTravel = 0.0;
		return milesToTravel;
	}
	public void setMilesToTravel(Double milesToTravel) {
		this.milesToTravel = milesToTravel;
	}
	public Date getMoveStart() {
		if (moveStart == null) moveStart = new Date();
		return moveStart;
	}
	public void setMoveStart(Date moveStart) {
		this.moveStart = moveStart;
	}
}
